package com.example.zhongcao.controller;


import com.example.zhongcao.exception.InvalidParam;
import com.example.zhongcao.exception.User.InvalidOpenid;
import com.example.zhongcao.service.UserService;
import com.example.zhongcao.utils.Message;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;


@CrossOrigin
@RestController
@RequestMapping("user")
public class UserController {
    @Resource
    UserService userService;

    @RequestMapping(value = "login", method = RequestMethod.GET)
    public Message login(@RequestParam("appid") String appid, @RequestParam("appsecret") String appsecret, @RequestParam("code") String code) {
        if (appid.equals("") || appid == null) {
            return Message.createErr("appid为空");
        } else if (appsecret.equals("") || appsecret == null) {
            return Message.createErr("appsecret为空");
        } else if (code.equals("") || code == null) {
            return Message.createErr("code为空");
        }
        String msg = "授权登录成功";
        try {
            //userService.codeToOpenid(appid,appsecret,code);
            return Message.createSuc(userService.codeToOpenid(appid, appsecret, code));
        } catch (IOException e) {
            e.printStackTrace();
            msg = "系统繁忙，授权登录失败";
            return Message.createErr(msg);
        } catch (InvalidOpenid e) {
            System.out.println(e.getErrmsg());
            return Message.createErr(e.getErrmsg());
        }
    }

    @RequestMapping(value = "sendQuestion",method = RequestMethod.POST)
    public Message rendQues(@RequestParam("openid") String openid, @RequestParam(value = "question", required = false) String question,
                            @RequestParam(value = "pics", required = false) List<MultipartFile> pics) {
        String msg = null;
        try {
            msg = "已经收到您的反馈啦，感谢您的宝贵意见！";
            userService.sendQuestion(openid, question, pics);
            return Message.createSuc(msg);
        } catch (InvalidParam e) {
            msg = e.getErrmsg();
            return Message.createErr(msg);
        }
    }
}



